package com.leyou.cart.controller;

import com.leyou.cart.entity.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author syf
 * @version 1.0
 * @date 2019/08/17 10:21
 * @Description TODO:购物车结构map<用户id，map<skuId,skuCart>>
 */
@RestController
@RequestMapping
public class CartController {

    @Autowired
    private CartService cartService;

    //添加购物车
    @PostMapping
    public ResponseEntity<Void> saveCart(@RequestBody Cart cart){
        cartService.saveCart(cart);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //获取购物车
    @GetMapping("/list")
    public ResponseEntity<List<Cart>> findCarts(){
        List<Cart> list = cartService.findCarts();
        return ResponseEntity.ok(list);
    }

    //把无登录购物车里的商品合并到已登录购物车里
    @PostMapping("/list")
    public ResponseEntity<Void> addCarts(@RequestBody List<Cart> carts){
        cartService.addCarts(carts);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
